package com.daijie.demo

import android.content.Context
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.readystatesoftware.chuck.ChuckInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory


class RetrofitManager(context: Context) {

    val cookieCache = SetCookieCache()
    val cookieJar = PersistentCookieJar(cookieCache, SharedPrefsCookiePersistor(context))

    val retrofit = {
        val builder = OkHttpClient.Builder()
            .addInterceptor(ChuckInterceptor(context))
            .cookieJar(cookieJar)

        Retrofit.Builder()
            .baseUrl("https://yiban.stu.edu.cn/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(builder.build())
            .build()
    }()

}